package com.manage.model.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.manage.common.enums.ResultCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.io.Serializable;

/**
 * @author WangZiMin
 * @version V1.0
 * @date 2022/2/21
 */
@Getter
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "状态码", notes = "默认0是成功")
    private int code;

    @ApiModelProperty(value = "响应信息", notes = "来说明响应情况")
    private String msg;

    //如果为空，则不返回
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "响应数据", notes = "响应的具体数据")
    private T data;

    public Result(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
        this.data = data;
    }

    public Result(ResultCode resultCode, String msg) {
        this(resultCode.getCode(), msg);
    }

    public Result(int code, String msg) {
        this(code, msg, null);
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> succee(T data) {
        return new Result<T>(data);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<T>(ResultCode.FAILED, msg);
    }

}
